package com.yuzhenxin.coursesystem.controller.teacher;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.framework.common.pojo.R;
import com.yuzhenxin.coursesystem.config.EndPoints;
import com.yuzhenxin.coursesystem.controller.admin.vo.UserQueryReqVO;
import com.yuzhenxin.coursesystem.controller.teacher.vo.TeacherCreateReqVO;
import com.yuzhenxin.coursesystem.entity.TeacherDO;
import com.yuzhenxin.coursesystem.listener.TeacherListener;
import com.yuzhenxin.coursesystem.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Tag(name = "教师管理")
@RestController
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @Operation(summary = "根据id查询教师信息")
    @GetMapping(value = EndPoints.TEACHERE_GETBYID)
    public R<TeacherDO> getById(@RequestParam("id") Long id) {
        TeacherDO teacherEntity = teacherService.getById(id);
        return R.success(teacherEntity);
    }

    @Operation(summary = "分页条件查询教师信息")
    @PostMapping(EndPoints.TEACHERE_SELECTPAGE)
    public R<IPage<TeacherDO>> selectPage(@RequestBody UserQueryReqVO queryDto) {
        IPage<TeacherDO> teacherIPage = teacherService.selectPage(queryDto);
        return R.success(teacherIPage);
    }

    @Operation(summary = "查询教师男女比例及总数")
    @GetMapping(EndPoints.TEACHER_SELECTCOUNT)
    public R<Map<String, Object>> selectCount() {
        Map<String, Object> teacherCounts = teacherService.selectCount();
        return R.success(teacherCounts);
    }

    @Operation(summary = "查询教师职称男女比例")
    @GetMapping(EndPoints.TEACHER_SELECTORDERBYTPT)
    public R<List<Map<String, Object>>> selectTeacherOrderByTpt() {
        List<Map<String, Object>> maps = teacherService.selectTeacherOrderByTpt();
        return R.success(maps);
    }

    @Operation(summary = "导出数据")
    @GetMapping(EndPoints.TEACHER_EXPORTDATA)
    public void exportData(HttpServletResponse response) {
        teacherService.exportData(response);
    }

    @Operation(summary = "添加教师")
    @PostMapping(EndPoints.TEACHER_INSERT)
    public R<?> insert(@Valid @RequestBody TeacherCreateReqVO reqVO) {
        teacherService.insert(TeacherDO.builder()
                .number(reqVO.getNumber())
                .password(reqVO.getPassword())
                .name(reqVO.getName())
                .sex(reqVO.getSex())
                .age(reqVO.getAge())
                .professionalTitle(reqVO.getProfessionalTitle())
                .build());
        return R.success();
    }

    @PostMapping(value = EndPoints.TEACHER_IMPORTDATA)
    public R<?> importData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), TeacherDO.class, new TeacherListener(teacherService)).sheet().doRead();
        return R.success();
    }

    @Operation(summary = "修改教师信息")
    @PostMapping(EndPoints.TEACHER_UPDATEBYID)
    public R<?> updateById(@RequestBody TeacherDO teacherEntity) {
        teacherService.updateById(teacherEntity);
        return R.success();
    }

    @Operation(summary = "删除单个教师信息")
    @DeleteMapping(EndPoints.TEACHER_REMOVEBYID)
    public R<?> removeById(@RequestParam("id") Integer studentId) {
        teacherService.removeById(studentId);
        return R.success();
    }

    @Operation(summary = "批量删除多个教师信息")
    @DeleteMapping(EndPoints.TEACHER_REMOVEBYIDS)
    public R<?> removeByIds(@RequestBody List<Integer> idList) {
        teacherService.removeByIds(idList);
        return R.success();
    }

}
